﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using Teamwish.Domain.Common;
using Teamwish.Domain.Models;
using Teamwish.Webapi.Filters;
using Teamwish.Webapi.Models;

namespace Teamwish.Webapi.Controllers
{
    [RedisToken]
    public class UserinfoController : ApiController
    {
        private EfDbContext db = DIFactory.Resolve<EfDbContext>();
              
        public SeverMsg GetUserinfo()
        {
            SeverMsg msg = new SeverMsg()
            {
                status=1,data= db.Userinfo
            };

            return msg;
        }

        
        
        public SeverMsg GetUserinfo(int id)
        {
            SeverMsg msg = new SeverMsg();
            msg.status = -1;

            Userinfo userinfo = db.Userinfo.Find(id);
            if (userinfo == null)
            {
                msg.message = "未找到用户";
                return msg;
            }
            msg.status = 1;
            msg.data = userinfo;        
            return msg;
        }

        
        [ResponseType(typeof(void))]
        public IHttpActionResult PutUserinfo(int id, Userinfo userinfo)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != userinfo.Id)
            {
                return BadRequest();
            }

            db.Entry(userinfo).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserinfoExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        
        [ResponseType(typeof(Userinfo))]
        public IHttpActionResult PostUserinfo(Userinfo userinfo)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Userinfo.Add(userinfo);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = userinfo.Id }, userinfo);
        }

        
        [ResponseType(typeof(Userinfo))]
        public IHttpActionResult DeleteUserinfo(int id)
        {
            Userinfo userinfo = db.Userinfo.Find(id);
            if (userinfo == null)
            {
                return NotFound();
            }

            db.Userinfo.Remove(userinfo);
            db.SaveChanges();

            return Ok(userinfo);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool UserinfoExists(int id)
        {
            return db.Userinfo.Count(e => e.Id == id) > 0;
        }
    }
}